package com.example.myapplication.management;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.myapplication.R;
import com.example.myapplication.ShuJu.ActivityListBean;
import com.example.myapplication.ShuJu.CategoryListBean;
import com.example.myapplication.ShuJu.ManagementBean;
import com.example.myapplication.apter.CtivityListApter;
import com.example.myapplication.apter.ManagementApter;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ManagementActivity extends AppCompatActivity {

    private ViewPager pa_llo;
    private Gson gson;
    private OkHttpClient okHttpClient;
    private Request request;
    private Handler handler;
    private ManagementBean managementBean;
    private ActivityListBean activityListBean;
    private RecyclerView re_list;
    private TabLayout tl_bar;
    private CategoryListBean categoryListBean ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_management);
        ivitview();
        //轮播图
        srndbean();
        //获取活动列表
       // ActivityList();
        //获取活动分类列表
        Category_list();
        sendActivityList(1);

    }

    private void Category_list() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/activity/category/list")
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                categoryListBean = gson.fromJson(string, CategoryListBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        for (CategoryListBean.DataBean datum : categoryListBean.getData()) {
                            TabLayout.Tab tab = tl_bar.newTab();
                            tab.setText(datum.getName());
                            tl_bar.addTab(tab);
                        }
                        Acquisitionsort();
                    }
                });
            }
        });
    }

    private void Acquisitionsort() {
        tl_bar.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int tabpos = tab.getPosition();
                String tabstr = tab.getText().toString();
                Log.d("tabpos=====",tabpos+"====");
                Log.d("tabstr=====",tabstr+"====");
                if(categoryListBean.getData().get(tabpos).getName().equals(tabstr)){
                    sendActivityList(categoryListBean.getData().get(tabpos).getId());
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void sendActivityList(Integer id) {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/activity/activity/list?recommend=N&pageNum=1&pageSize=8&categoryId="+id)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                activityListBean = gson.fromJson(string, ActivityListBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        bingactivityListBean();
                    }
                });
            }
        });
    }

    /*private void ActivityList() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/activity/activity/list")
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                activityListBean = gson.fromJson(string, ActivityListBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        bingactivityListBean();
                    }
                });
            }
        });
    }*/

    private void bingactivityListBean() {
        CtivityListApter ctivityListApter = new CtivityListApter(ManagementActivity.this,activityListBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ManagementActivity.this);
        re_list.setLayoutManager(linearLayoutManager);
        re_list.setAdapter(ctivityListApter);
    }

    private void srndbean() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/activity/rotation/list")
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                managementBean = gson.fromJson(string, ManagementBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                       onclikbean();
                    }
                });
            }
        });
    }

    private void onclikbean() {
        ManagementApter managementApter = new ManagementApter(ManagementActivity.this,managementBean.getRows());
        pa_llo.setAdapter(managementApter);
        mHandler.sendEmptyMessageDelayed(0, 3 * 1000);
    }

    private void ivitview() {
        pa_llo = findViewById(R.id.pa_llo);
        re_list = findViewById(R.id.re_list);
        tl_bar = findViewById(R.id.tl_bar);
        okHttpClient = new OkHttpClient();
        gson = new Gson();
        handler = new Handler();
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (msg.what == 0) {
                int index = pa_llo.getCurrentItem();
                index = (index + 1) % 4;
                pa_llo.setCurrentItem(index);
                mHandler.sendEmptyMessageDelayed(0, 3 *1000);
            }
        }
    };
}